//
//  HELCouponVC.m
//  NursingAssociation
//
//  Created by macOS on 2017/3/5.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCouponVC.h"
#import "HELCouponModel.h"
#import "HELCouponListCell.h"
#import "BFPaperButton.h"
#import "UIButton+AnimationBtn.h"
@interface HELCouponVC ()


@property(strong,nonatomic) BFPaperButton *headerBtn;

@end

@implementation HELCouponVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64- 49);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCouponListCell" bundle:nil] forCellReuseIdentifier:@"HELCouponListCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
    self.title =@"我的优惠券";
    weakSelf_define;
  
    if (!self.coupons_arr) {
        [self.tableView wsj_HeaderRefreshBlock:^{
            [weakSelf getMyCouponList:YES];
        }];
        [self.tableView wsj_FooterRefreshBlock:^{
            [weakSelf getMyCouponList:NO];
        }];
        [weakSelf getMyCouponList:YES];
        [self.view addSubview:self.headerBtn];
        self.headerBtn.evnetblock = ^(){
            
            [weakSelf getCouponRequest];
        };
        
    }else{
        [self.view addSubview:self.headerBtn];
        [self.headerBtn setTitle:@"不使用优惠券" forState:0];
        [self.headerBtn addTarget:self action:@selector(notUse) forControlEvents:UIControlEventTouchUpInside];
    
    }
 
}


- (void)notUse{
    
    if (self.callBack) {
        self.callBack(-2);
    }
    [self.navigationController popViewControllerAnimated:YES];


}

- (void)setCoupons_arr:(NSArray<HELCouponModel *> *)coupons_arr{
    _coupons_arr = coupons_arr;
    [self.dataArray addObjectsFromArray:coupons_arr];
}

- (void)viewWillAppear:(BOOL)animated{
 

}

-(BFPaperButton *)headerBtn{
    if (!_headerBtn) {
        _headerBtn =[BFPaperButton buttonWithType:UIButtonTypeCustom];
        _headerBtn.frame = CGRectMake(20, 8, kScreenWidth - 40, 49);
        [_headerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _headerBtn.isRaised = YES;
        _headerBtn.titleFont = [UIFont systemFontOfSize:17];
        [_headerBtn setTitle:@"新用户优惠领取" forState:UIControlStateNormal];
        [_headerBtn setBackgroundColor:kPinchColor];
       
    }
    return _headerBtn;
}

-  (void)addHeaderBtn{
   
    BFPaperButton *btn =[BFPaperButton buttonWithType:UIButtonTypeCustom];
    
    btn.titleFont = [UIFont systemFontOfSize:17];
    [btn setTitle:@"新用户优惠领取" forState:UIControlStateNormal];
    [btn setBackgroundColor:kMainColor];
    [btn addTarget:self action:@selector(getCouponRequest) forControlEvents:UIControlEventTouchUpInside];
    
    

}


- (void)getCouponRequest{
    
    
    
      weakSelf_define;
      [[HELNetworkManager sharedMg] GET:helGetCoouponByNewuserActivety parameters:nil success:^(id responseObject) {
          weakSelf.headerBtn.endAniblock  = ^(){
             
              [weakSelf getMyCouponList:YES];
          
          };
          
          
      } failure:^(NSString *err) {
           weakSelf.headerBtn.endAniblock = nil;
      }];
    
    
    

}


- (void)getMyCouponList:(BOOL)isRecover{
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    
    
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:kCouponList parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
//            [weakSelf.dataArray addObjectsFromArray:[HELCouponModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            NSArray *tem_arr =responseObject[@"data"][@"data"];
            if ([tem_arr isKindOfClass:[NSArray class]]) {
                [tem_arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
                    [dic addEntriesFromDictionary:obj[@"couponMember"]];
                     [dic addEntriesFromDictionary:obj[@"couponActivityIntro"]];
                    
                    [weakSelf.dataArray addObject:[HELCouponModel mj_objectWithKeyValues:dic]];
                }];
                
            }
            
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  136;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELCouponListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELCouponListCell"];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
    if (self.callBack) {
        self.callBack(indexPath.row);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}




@end
